package com.xinsoft.controller.device;

import cn.hutool.core.util.StrUtil;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusDeviceBreakdownDTO;
import com.xinsoft.entity.dto.CusDeviceBreakdownPageParam;
import com.xinsoft.entity.dto.CusRepaireDTO;
import com.xinsoft.service.CusDeviceBreakdownService;
import com.xinsoft.service.CusRepaireService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cus/deviceBreakdown")
public class CusDeviceBreakdownController {

    @Autowired
    private CusDeviceBreakdownService cusDeviceBreakdownService;


    @Autowired
    private CusRepaireService cusRepaireService;


    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody CusDeviceBreakdownDTO cusDeviceBreakdownDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(cusDeviceBreakdownDTO);
        if(StrUtil.isNotEmpty(cusDeviceBreakdownDTO.getBreakdownCode())){
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.DEVICE_BREAKDOWN_DOCUMENT);
        }
        return Response.succeed(cusDeviceBreakdownService.addCusDeviceBreakdown(cusDeviceBreakdownDTO));
    }

    /**
     * 更新
     */
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody  CusDeviceBreakdownDTO cusDeviceBreakdownDTO) {
        // 参数校验
        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.DEVICE_BREAKDOWN_DOCUMENT);
        return Response.succeed(cusDeviceBreakdownService.editCusDeviceBreakdown(cusDeviceBreakdownDTO));
    }

    /**
     * 提交
     */
    @PostMapping("/submit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> submit(@RequestBody CusDeviceBreakdownDTO cusDeviceBreakdownDTO) {
        // 参数校验

        cusDeviceBreakdownDTO.setRepaireStatus(Const.RepairStatus.SUBMIT);
        if(cusDeviceBreakdownDTO.getId()==null){
            //保存
            if(StrUtil.isNotEmpty(cusDeviceBreakdownDTO.getBreakdownCode())){
                sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.DEVICE_BREAKDOWN_DOCUMENT);
            }
            return Response.succeed(cusDeviceBreakdownService.addCusDeviceBreakdown(cusDeviceBreakdownDTO));
        }
        return Response.succeed(cusDeviceBreakdownService.editCusDeviceBreakdown(cusDeviceBreakdownDTO));
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(cusDeviceBreakdownService.getCusDeviceBreakdown(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        cusDeviceBreakdownService.del(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusDeviceBreakdownPageParam cusDeviceBreakdownPageParam) {
        return Response.succeed(cusDeviceBreakdownService.findPageListByParam(cusDeviceBreakdownPageParam));
    }

    /**
     * 暂存维修处理
     */
    @PostMapping("/addDeal")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addDeal(@RequestBody CusRepaireDTO cusRepaireDTO) {
        cusRepaireDTO.setRepaireStatus(Const.RepairRecordStatus.PROCESSING);
        return Response.succeed(cusRepaireService.dealAddCusRepaire(cusRepaireDTO));
    }

    /**
     * 编辑维修处理
     */
    @PostMapping("/editDeal")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> editDeal(@RequestBody CusRepaireDTO cusRepaireDTO) {
        cusRepaireDTO.setRepaireStatus(Const.RepairRecordStatus.PROCESSING);
        return Response.succeed(cusRepaireService.dealEditCusRepaire(cusRepaireDTO));
    }

    /**
     * 提交维修处理
     */
    @PostMapping("/submitDeal")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> submitDeal(@RequestBody CusRepaireDTO cusRepaireDTO) {
        cusDeviceBreakdownService.submitDeal(cusRepaireDTO);
        return Response.succeed();
    }

    /**
     * 终止维修处理
     */
    @GetMapping("/terminated")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> terminated(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        cusDeviceBreakdownService.changeStatus(
                id,
                Const.RepairStatus.REPORT_TERMINATED,
                Const.RepairRecordStatus.TERMINATED,null);
        return Response.succeed();
    }


    /**
     * 转派维修处理
     */
    @PostMapping("/transfer")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> transfer(@RequestBody CusDeviceBreakdownDTO cusDeviceBreakdownDTO) {

        cusDeviceBreakdownService.changeStatus(
                cusDeviceBreakdownDTO.getId(),
                Const.RepairStatus.SUBMIT,
                Const.RepairRecordStatus.TRANSFERRED,
                cusDeviceBreakdownDTO.getRepaireUserId());
        return Response.succeed();
    }


    /**
     * 获取维修记录详情用于维修处理
     */
    @GetMapping("/getRepairDetailByBreakDownId")
    public Response<?> getRepairDetailByBreakDownId(@RequestParam(value = "breakdownId", defaultValue = "", required = true) Integer breakdownId) {
        return Response.succeed(cusDeviceBreakdownService.getRepairDetailByBreakDownId(breakdownId));
    }

}
